package vcp.pdf;

/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.awt.PdfGraphics2D;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;

import db.parking.beans.ParkingLot;

import java.awt.Color;
import java.awt.geom.Ellipse2D;

import javax.swing.JFileChooser;


/**
 * The resulting PDF of parking lots
 *
 */
public class LocationsState {
	/** The resulting PDF. */

	private static final String EXTENSION = ".pdf";
	private int pdfStatus=0;

	public int getPdfStatus() {
		return pdfStatus;
	}

	public void setPdfStatus(int pdfStatus) {
		this.pdfStatus = pdfStatus;
	}

	/** paint some shape, square, x, circle fill square
	 * @param g
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 * @param color
	 */
	private void paintShape(Graphics g, int x, int y,/* int z, */int w, int h,
			int color) {// get posiotion x,y and status //help function

		Graphics2D g2 = (Graphics2D) g;
		if (color == 0) { // empty and good
			g.setColor(Color.GREEN.darker());
			g.drawRect(x, y, w, h); // draw the rectangle
		} else if (color == 1) { // full
			g.setColor(Color.GREEN.darker().darker());
			g.drawRect(x, y, w, h); // draw the rectangle
			g.setColor(Color.GREEN);
			g.fillRect(x+1, y+1, w-2, h-2);			
			
			
		} else if (color == 2) {
			g.setColor(Color.red);
			int t=7;
			g.drawLine(x, y, x+t, y+t);
			g.drawLine(x, y+t, x+t, y);
		} else if (color == 3) {
			g.setColor(Color.blue.brighter());
			Ellipse2D.Double circle = new Ellipse2D.Double(x, y, 8, 8);
			g2.fill(circle);
			
		}

	}
	
	
	/**
	 * Creates a Symbols meanning map.
	 * 
	 * @param filename
	 *            the path to the new PDF document
	 * @throws DocumentException
	 * @throws IOException
	 */
	private void drawSymbolsMeaning(Graphics g, PdfWriter writer){
		Graphics2D g2 = (Graphics2D) g;
		g.setColor(Color.black);
		g.drawRect(5, 120, 140, 28); // draw the rectangle*/

		
		
		//available place//
		g.setColor(Color.GREEN.darker());
		g.drawRect(40, 125, 7, 7); // draw the rectangle*/
		

		//full place//
		g.setColor(Color.black.darker());
		g.setColor(Color.GREEN.darker());
		g.drawRect(40, 138, 7, 7); // draw the rectangle*/
		g.setColor(Color.GREEN);
		g.fillRect(41,139, 5, 5); 
		
		
		
		//saved place//
		g.setColor(Color.blue.brighter());
		Ellipse2D.Double circle = new Ellipse2D.Double(130, 125, 8, 8);
		g2.fill(circle);
		
		//Disabled place//	
		int t=7,x=130,y=138;
		g.setColor(Color.red);
		g.drawLine(x, y, x+t, y+t);
		g.drawLine(x, y+t, x+t, y);
		
		g.setColor(Color.BLACK);
		absText("Empty Place", 8, 20, writer,8);
		absText("Full Place", 8, 8, writer,8);
		absText("Saved Place", 95, 20, writer,8);
		absText("Disabled Place", 95, 8, writer,8);


	}

		
		
	
	/**
	 * Creates a PDF document.
	 * 
	 * @param filename
	 *            the path to the new PDF document
	 * @throws DocumentException
	 * @throws IOException
	 */

	public void createPdf(ParkingLot pL) throws IOException,
			DocumentException {
		int l = pL.getX();
		int m = pL.getY();
		int n = pL.getZ();
		int y = 18;
		int y0 = 0;

		JFileChooser saveFile = new JFileChooser(); // save as dialog
		int option = saveFile.showSaveDialog(null);
		
		if(option==JFileChooser.CANCEL_OPTION){
			setPdfStatus(1);
			return;
		}else if(option==JFileChooser.ERROR_OPTION){
			setPdfStatus(0);
			return;
		}
		
		File selectedFile = saveFile.getSelectedFile();
		Document document = null;
		PdfWriter writer = null;
        try {
            String fileName = selectedFile.getCanonicalPath();
            if (!fileName.endsWith(EXTENSION)) {
                selectedFile = new File(fileName + EXTENSION);
            }
            document = new Document(new Rectangle(150, 150));
    		
    		
            writer = PdfWriter.getInstance(document, new FileOutputStream(selectedFile));
        } catch (IOException e) {
            e.printStackTrace();
        }

		
		
		
		// step 2
		try {
			// step 3
			document.open();
			// step 4
			PdfContentByte canvas = writer.getDirectContent();

			Graphics2D g2 = new PdfGraphics2D(canvas, 800, 150);
			int lotNum=pL.getLotID();
			String s="Postions Status For Lot Number "+ lotNum;
			absText(s, 45, 145, writer,12);

			for (int i = 0; i < pL.getZ(); i++){
				if(i==0)
					absText("First Floor =>", 10, 120 - 35 * i, writer,12);		//set first floor
				else if(i==1)
					absText("Second Floor =>", 10, 120 - 35 * i, writer,12);	//set first floor
				else 
					absText("Third Floor =>", 10, 120 - 35 * i, writer,12);		//set first floor
			}
			g2.setColor(Color.gray);

			for (int i = 0; i < pL.getZ(); i++) {
				y0 += 34;
				for (int j = 0; j < pL.getY(); j++) {
					int x = 60;
					for (int k = 0; k < pL.getX(); k++) {
						paintShape(g2, x, y, 7, 7, pL.getLocation(k, j, i));
						x += 10;
					}
					x = 20;
					y += 9;
				}
				y += (9 - i);
			}
			drawSymbolsMeaning(g2,writer); 
			g2.dispose();
			// step 5
			document.close();
			setPdfStatus(2);
			
		}
		

		catch (Exception e) {
			System.out.print(e + "in: createPdf ");
			document.close();
		}
	}

	/**Add some text to pdf File
	 * @param text
	 * @param x
	 * @param y
	 * @param writer
	 * @param size
	 */
	private static void absText(String text, int x, int y, PdfWriter writer,float size) {
		try {
			PdfContentByte cb = writer.getDirectContent();
			BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN,
					BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
			cb.setFontAndSize(bf,size);
			cb.saveState();
			cb.beginText();
			cb.moveText(x, y); 		//set postion of text
			cb.setFontAndSize(bf, 5);	//set font of text
			cb.showText(text);
			cb.endText();
			cb.restoreState();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}